package model

import (
	"src/config"
	"src/util/gps"
	"strconv"
)

var BusJudgeState [config.BusNum][config.RouteNum * 2]int

func ChangeRouteIn18() { //每天下午6点执行的任务
	//下午六点之后0号线（喻园-校大门）走1号线（韵苑-南大门）
	config.Rou.BusRoute["HS659010013_14号车"] = 1
	config.Rou.BusRoute["HS659010017_13号车"] = 1
	config.Rou.BusRoute["HS659010016_12号车"] = 1

	config.Rou.BusRoute["HS659010003_11号车"] = 1
}

func ChangeRouteIn6() { //每天凌晨6点执行的任务
	//将每天下午六点之后0号线（喻园-校大门）走1号线（韵苑-南大门）的改动 改回来
	config.Rou.BusRoute["HS659010013_14号车"] = 0
	config.Rou.BusRoute["HS659010017_13号车"] = 0
	config.Rou.BusRoute["HS659010016_12号车"] = 0

	config.Rou.BusRoute["HS659010003_11号车"] = -1
}

func ChangeRouteEveryMonth() { //每月执行的任务
	//将1号线与2号线调换
	config.Rou.BusRoute["HS659010010_3号车"] = 1
	config.Rou.BusRoute["HS659010022_2号车"] = 1
	config.Rou.BusRoute["HS659010011_4号车"] = 1
	config.Rou.BusRoute["HS659010008_5号车"] = 1
	config.Rou.BusRoute["HS659010025_1号车"] = 1

	config.Rou.BusRoute["HS659010014_8号车"] = 2
	config.Rou.BusRoute["HS659010026_7号车"] = 2
	config.Rou.BusRoute["HS659010012_6号车"] = 2
	config.Rou.BusRoute["HS659010006_9号车"] = 2
	config.Rou.BusRoute["HS659010004_10号车"] = 2
}

func init() {
	for _, j := range BusJudgeState {
		for i, _ := range j {
			j[i] = 0
		}
	}
}

func JudgeRoute(data Buses) {
	for _, i := range data.Data {
		if config.Rou.BusRoute[i.PlateNumber] == -1 {
			id := config.Busmessages.BusToId[i.PlateNumber]
			for rouid, _ := range BusJudgeState[id] {
				Lon, err := strconv.ParseFloat(i.Gps[0], 64) //WGS经度
				if err != nil {
					println(err.Error())
					continue
				}
				Lat, err := strconv.ParseFloat(i.Gps[1], 64) //WGS维度
				if err != nil {
					println(err.Error())
					continue
				}
				var StartPointDis float64 //与路线起始点距离
				var EndPointDis float64   //与路线终点距离
				if rouid%2 == 0 {
					StartPointDis = gps.GetDistance(Lat, Lon, config.Rou.Routes[rouid/2].Points[0].WGS84Lat, config.Rou.Routes[rouid/2].Points[0].WGS84lon)
					EndPointDis = gps.GetDistance(Lat, Lon, config.Rou.Routes[rouid/2].Points[len(config.Rou.Routes[rouid/2].Points)-1].WGS84Lat, config.Rou.Routes[rouid/2].Points[len(config.Rou.Routes[rouid/2].Points)-1].WGS84lon)
				} else {
					StartPointDis = gps.GetDistance(Lat, Lon, config.Rou.Routes[rouid/2].OppositePoints[0].WGS84Lat, config.Rou.Routes[rouid/2].OppositePoints[0].WGS84lon)
					EndPointDis = gps.GetDistance(Lat, Lon, config.Rou.Routes[rouid/2].OppositePoints[len(config.Rou.Routes[rouid/2].OppositePoints)-1].WGS84Lat, config.Rou.Routes[rouid/2].OppositePoints[len(config.Rou.Routes[rouid/2].OppositePoints)-1].WGS84lon)
				}
				if BusJudgeState[id][rouid] == 0 && StartPointDis <= 10 {
					BusJudgeState[id][rouid] = 1
				} else if BusJudgeState[id][rouid] == 1 && EndPointDis <= 10 {
					config.Rou.BusRoute[i.PlateNumber] = rouid / 2
					for i1, _ := range BusJudgeState[id] {
						BusJudgeState[id][i1] = 0
					}
					break
				} else if BusJudgeState[id][rouid] == 1 {
					_, _, _, Distance := findStation(Lat, Lon, config.Rou.Routes[rouid/2], rouid%2)
					if Distance > 10 {
						BusJudgeState[id][rouid] = 0
					}
				}
			}
		}
	}
}
